/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import com.sun.tools.doclint.Entity;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
 *
 * @author root
 */
public class FtpTools {

    private static Logger logger = Logger.getLogger(FtpTools.class);

    //计数下载1000个json文件
    public List<String> getJsonFiles(String url, int port, String username, String password,
            String remotePath, String filetype, String namespace) {
        logger.info("url:" + url + ", port: " + port + ",username: " + username + ",password: " + password + ",remotepath: " + remotePath);
        List<FTPFile> txtNameList = new ArrayList<FTPFile>();
        List<FTPFile> okList = new ArrayList<FTPFile>();
        List<FTPFile> dwdList = new ArrayList<FTPFile>();
        List<String> finalList = new ArrayList<String>();
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            reply = ftp.getReplyCode();
            logger.info("reply: " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                logger.info("if (!FTPReply.isPositiveCompletion(reply)) {");
                ftp.disconnect();
                logger.info(url + " ftp login err!username=" + username + ",password=" + password);
                return finalList;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            boolean changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录
            logger.info("changeWorkingDirectory " + remotePath + " return " + changeWorkingDirectory);

            FTPFile[] fs = ftp.listFiles();
            logger.debug(url + " ftp list size is " + fs.length);

            for (FTPFile ff : fs) {
                if (PatternTools.matchStr(filetype, ff.getName())) {
                    if (ff.getName().endsWith(".dwd")) {
                        dwdList.add(ff);
                    } else if (ff.getName().endsWith(".ok")) {
                        okList.add(ff);
                    } else {
                        txtNameList.add(ff);
                    }
                }
            }

            int max = Integer.parseInt(PropsFiles.getValue("jsonCount"));
            int sum = 0;
            for (int i = 0; i < txtNameList.size(); i++) {
                if (PatternTools.matchStr(filetype, txtNameList.get(i).getName())) {
                    String tmpokname = txtNameList.get(i).getName() + ".ok";
                    for (int j = 0; j < okList.size(); j++) {
                        if (tmpokname.equals(okList.get(j).getName())) {
                            boolean flag = false;
                            for (int k = 0; k < dwdList.size(); k++) {
                                String tmpdwdname = txtNameList.get(i).getName() + "." + namespace + ".dwd";
                                if (tmpdwdname.equals(dwdList.get(k).getName())) {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag && sum < max) {
                                finalList.add(txtNameList.get(i).getName());
                                sum++;
                            }
                        }
                    }
                }
            }

            for (String ff : finalList) {
                logger.debug(url + " finalList contains:" + ff);
            }

            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp download err " + e.getMessage(), e);
            return finalList;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    logger.error(ioe.getMessage() + ioe);
                }
            }
        }
        logger.info(url + " ftp download finish!");
        return finalList;
    }
    public List<String> getJsonFiles_new(String url, int port, String username, String password,
            String remotePath, String filetype, String namespace) {
        logger.info("url:" + url + ", port: " + port + ",username: " + username + ",password: " + password + ",remotepath: " + remotePath);
        List<String> txtNameList = new ArrayList<String>();
        HashMap<String,String> okmap = new HashMap<String,String>();
        HashMap<String,String> dwdmap = new HashMap<String,String>();
        List<String> finalList = new ArrayList<String>();
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            reply = ftp.getReplyCode();
            logger.info("reply: " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                logger.info("if (!FTPReply.isPositiveCompletion(reply)) {");
                ftp.disconnect();
                logger.info(url + " ftp login err!username=" + username + ",password=" + password);
                return finalList;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            boolean changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录
            logger.info("changeWorkingDirectory " + remotePath + " return " + changeWorkingDirectory);

            FTPFile[] fs = ftp.listFiles();
            logger.debug(url + " ftp list size is " + fs.length);

            for (FTPFile ff : fs) {
                if (PatternTools.matchStr(filetype, ff.getName())) {
                    if (ff.getName().endsWith(".dwd")) {
//                        dwdList.add(ff.getName());
                        dwdmap.put(ff.getName(), ff.getName());
                    } else if (ff.getName().endsWith(".ok")) {
                        okmap.put(ff.getName(), ff.getName());
                    } else {
                        txtNameList.add(ff.getName());
                    }
                }
            }

            int max = Integer.parseInt(PropsFiles.getValue("jsonCount"));
            int sum = 0;
            for (String txtName : txtNameList) {
                if (PatternTools.matchStr(filetype, txtName)) {
                    String tmpokname = txtName + ".ok";
                    if (okmap.containsKey(tmpokname)) {
                        String tmpdwdname = txtName + "." + namespace + ".dwd";
                        if (!dwdmap.containsKey(tmpdwdname) ) {
                            if (sum < max) {
                                finalList.add(txtName);
                                sum++;
                            }else{
                                break;
                            }
                            
                        }
                    }
                }
            }

            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp download err " + e.getMessage(), e);
            return finalList;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    logger.error(ioe.getMessage() + ioe);
                }
            }
        }
        logger.info(url + " ftp download finish!");
        return finalList;
    }
    
}
